import { NextResponse, type NextRequest } from 'next/server'; import { getCustomerUser } from '@/app/lib/lumen-auth'; import { commerceDb } from '@/app/lib/commerce-db'; import { sendEmail } from '@/app/lib/email'; import { env } from '@/app/lib/env'; import { displayOrderId } from '@/app/lib/orders'; import { customerApprovedAdminNotificationHtml } from '@/app/lib/order-emails'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // Customer approves the previewed portrait. Payment was collected // up-front at /checkout, so no Stripe charge happens here — we just // transition awaiting_approval → approved and notify the studio. The // admin then does one final review and clicks "Send to printer" in // the admin panel, which creates the Prodigi order. Print never // fires automatically from a customer click — admin always has a // final go/no-go. export async function POST( req: NextRequest, ctx: { params: Promise<{ id: string }> }, ) { const { id } = await ctx.params; const user = await getCustomerUser(); if (!user) return NextResponse.json({ error: 'unauthorized' }, { status: 401 }); let body: { termsAccepted?: boolean; generationId?: unknown } | null; try { body = await req.json(); } catch { return NextResponse.json({ error: 'invalid json' }, { status: 400 }); } if (body?.termsAccepted !== true) { return NextResponse.json({ error: 'You must accept the no-refund terms to approve.' }, { status: 400 }); } const admin = commerceDb(); const { data: order } = await admin .from('orders') .select('id, user_id, user_email, status, price_cents, current_generation_id') .eq('id', id) .single(); if (!order) return NextResponse.json({ error: 'order not found' }, { status: 404 }); if (order.user_id !== user.id) return NextResponse.json({ error: 'forbidden' }, { status: 403 }); const generationId = body.generationId; if (typeof generationId !== 'string' || generationId !== order.current_generation_id) { return NextResponse.json({ error: 'The proof has changed. Reload to review the current proof.' }, { status: 409 }); } if (order.status === 'approved') return NextResponse.json({ ok: true, idempotent: true }); if (order.status !== 'awaiting_approval') { return NextResponse.json({ error: 'This proof is no longer open for approval. Reload to see the latest status.' }, { status: 409 }); } const { data: claimed, error: updErr } = await admin.from('orders').update({ status: 'approved', terms_accepted_at: new Date().toISOString(), }).eq('id', id).eq('status', 'awaiting_approval').eq('current_generation_id', generationId).select('id'); if (updErr) { return NextResponse.json({ error: `update failed: ${updErr.message}` }, { status: 500 }); } if (!claimed || claimed.length === 0) { return NextResponse.json({ error: 'The proof changed while you were approving it. Reload the order to continue.' }, { status: 409 }); } // Notify the studio so admin knows it's time to do a final review // and send to the printer. Best-effort — don't fail the approve on // email errors. await sendEmail({ to: env.site.studioEmail(), subject: `Approved & awaiting print · ${displayOrderId(order.id)} — ${order.user_email}`, html: customerApprovedAdminNotificationHtml(order.id, order.user_email, order.price_cents), text: `${order.user_email} approved order ${order.id}. Review in admin and send to printer: ${env.site.url()}/admin/orders/${order.id}`, }).catch(() => {}); return NextResponse.json({ ok: true }); }